//
//  MainViewController.swift
//  Calculator
//  主界面
//  Created by 白瑾浩 on 2022/1/12.
//

import UIKit
import SnapKit

class MainViewController: UIViewController, BoardButtonInputDelegate{
    
    var screen:Screen?
    var calculatorEngine:CalculatorEngine = CalculatorEngine()
    var isNew = true
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //self.view.backgroundColor = .blue
    
        print( "MainViewController" )
        let board = BoardView()
        
        board.delegate = self
        
        self.view.addSubview( board )
        
        board.snp.makeConstraints { make in
            make.left.right.bottom.equalTo( 0 )
            make.height.equalTo( self.view ).multipliedBy( 5/8.0 )
        }
        
        
        
        screen = Screen()
        
        self.view.addSubview( screen! )
    
        screen?.snp.makeConstraints { make in
            make.left.right.top.equalTo( 0 )
            //make.height.equalTo( self.view ).multipliedBy( 3/8.0 )
            make.bottom.equalTo( board.snp.top )
        }
        
    }
    
    
    
    func boardButtonClick(Content: String) {
        if Content == "AC" || Content == "Del" || Content == "="{
            switch Content{
            case "AC":
                screen?.clearContent()
                screen?.refreshHistory()
            case "Del":
                screen?.deleteInput()
                screen?.refreshHistory()
            case "=":
                
                let res = calculatorEngine.calculatEquation(equation: screen!.inputString )
                
                screen?.refreshHistory()
                
                screen?.clearContent()
                
                screen?.inputContent(content:  String( res ) )
                
                isNew = true
                
            
            default:
                screen?.refreshHistory()
            }
            
            return
        }
        if isNew {
            screen?.clearContent()
            isNew = false
        }
        screen?.inputContent(content:  Content )
    }

   


}
